package com.itcast.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.dao.PermissionMapper;
import com.itcast.dao.RoleMapper;
import com.itcast.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.itcast.health.pojo.Permission;
import com.itcast.health.pojo.Role;
import com.itcast.health.pojo.User;

import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override

    public User findByUser(String username) {
        User user = userMapper.findUserByname(username);
        if (user == null) {
            return null;
        }
        Integer userId = user.getId();
        Set<Role> roles = roleMapper.findRoleByUserid(userId);
        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {
                Integer roleId = role.getId();
                Set<Permission> permissions =permissionMapper.findPermissonByRoleid(roleId);
                if (permissions != null && permissions.size() > 0) {
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }
        return user;

    }
}
